*version 8.2
capture clear
capture log close
set more off
set mem 1000m
set mat 800


****IATI data:
insheet using "IATI_judicial reforms_coding.csv"
ren recipientcountry countryname

*year:
gen temp=substr(startactual,7,4)
replace temp=substr(startplanned,7,4) if temp==""
gen year=real(temp)
drop temp
*browse startactual startplanned year 


***browse world bank projects:
*browse if reportingorg=="World Bank Group"
****None of these projects are in the World Bank dataset except:
*44000-P089733	World Bank Group	The judicial reform support project for the Russian Federation aims to strengthen judicial transparency and efficiency in courts financed by the judicial reform support project. The project consists of the following components: A. This componen	30/06/2005	15/02/2007	International Bank for Reconstruction and Development	Russian Federation	0		1	1		2007	
*44000-P083904	World Bank Group	The development objective of the Justice Services Strengthening Project for Colombia is to strengthen the capacity of the judiciary and the ministry of interior and justice (MIJ) to deliver timely, efficient, effective, and quality dispute reso	20/06/2005	17/12/2009	International Bank for Reconstruction and Development	Colombia	1.84E+07			1		2009	
*We take them out from here to avoid double-counting
drop if iatiidentifier=="44000-P089733"|iatiidentifier=="44000-P083904"

*Note: USAID is not in IATI data. It has to be downloaded directly from USAID website.

count
***4659 judicial reforms
sort year
***Between 1996 and 2016
sum totalcommitment if totalcommitment>0
***average: 5.5 million dollars
sum totalcommitment if countryname!=""&totalcommitment>0&(reform_access==1|reform_speed==1|reform_quality==1)
*egen sum_totalcommitment=sum(totalcommitment) if (reform_access==1|reform_speed==1|reform_quality==1)
egen sum_totalcommitment=sum(totalcommitment) if countryname!=""&(reform_access==1|reform_speed==1|reform_quality==1)&reportingorg!="World Bank Group"

***total: 3.2 Billion dollars
*drop sum_totalcommitment

*browse if totalcommitment>=701000000

***GB in pounds
replace totalcommitment=totalcommitment*1.5 if reportingorg=="Department for International Development"|reportingorg=="Foreign & Commonwealth Office"
***EU in euros:
replace totalcommitment=totalcommitment*1.3 if reportingorg=="Commission of the European Communities"|reportingorg=="European Commission - Development and Cooperation-EuropeAid"


*browse countryname year reform_access reform_speed reform_quality

sort reportingorg
*browse reportingorg  participatingorgfunding if reportingorg!=reportingorg[_n-1]
count if  reportingorg!=reportingorg[_n-1]
*45 organizations


sort countryname year
save "IATI_judicial reforms_coding_clean.dta", replace
clear


insheet using "World Bank_judicial reforms.csv"
*browse countryname year reform_access reform_speed reform_quality totalamt lendprojectcost


drop if countryname==""

local list_type_reform access speed quality
foreach var of local list_type_reform{
	ren reform_`var' reform_`var'_wb
	}
	
destring lendprojectcost, gen(totalcommitment_wb) ignore(";")	

***note: replace totalcommitment_wb=0 if we didn't code it as a judicial reform:
replace totalcommitment_wb=0 if reform_access==.&reform_speed==.&reform_quality==.

count
***50 judicial reforms
sort year
***Between 2006 and 2015
sum totalcommitment_wb
***average: 30 million dollars, max 300 million dollars
egen sum_wb=sum(totalcommitment_wb)
***total: 1.5 Billion dollars
drop sum_wb

ren id iatiidentifier

sort countryname year

save "World Bank_judicial reforms_clean.dta", replace
clear


***Doing business data:
insheet using "Doing business reforms_clean.csv"

*browse countryname year reform_access reform_speed reform_quality

drop if countryname==""

local list_type_reform access speed quality
foreach var of local list_type_reform{
	ren reform_`var' reform_`var'_db
	}
	
	
sort countryname year
save "Doing business reforms_clean.dta", replace
clear



****USAID data:

insheet using "USAID_Rule of Law and Human Rights_clean.csv"

keep if reform_access==1|reform_speed==1|reform_quality==1

ren awardlocationname countryname
ren awardidentifier iatiidentifier


keep countryname iatiidentifier reform_access reform_speed reform_quality awardtotalestimatedvalue awarddatestartdatedate
destring awardtotalestimatedvalue, gen(totalcommitment_usaid) ignore(",")	
drop awardtotalestimatedvalue


*browse

*year:
gen temp=substr(awarddatestartdatedate,7,4)
gen year=real(temp)
drop temp
 
keep countryname iatiidentifier  year reform_access reform_speed reform_quality totalcommitment_usaid

local list_type_reform access speed quality
foreach var of local list_type_reform{
	ren reform_`var' reform_`var'_usaid
	}


count
***61 judicial reforms
***But 4 are quite old (before 2007)
***So 57 judicial reforms that fit the criteria, ie that are started after the entreprise surveys
sort year
***Between 2008 and 2016
sum totalcommitment_usaid
***average: 14 million dollars, max 66 million dollars
egen sum_usaid=sum(totalcommitment_usaid)
***total: 676 million dollars
drop sum_usaid
sort countryname year
save "USAID_judicial reforms_clean.dta", replace
clear

***Merging:

use "Enterprise surveys_clean2.dta"
append using "IATI_judicial reforms_coding_clean.dta"
append using "World Bank_judicial reforms_clean.dta"
append using "Doing business reforms_clean.dta"
append using "USAID_judicial reforms_clean.dta"
sort countryname year h7a

***Generate a post=0 indicator for the period before the reform:
replace post=post[_n-1] if countryname==countryname[_n-1]&post[_n-1]==0&post==.

***another idea: reform_time_eligible is in between baseline and endline
egen year_baseline=min(year) if h7a!=., by(countryname)
egen year_baseline2=mean(year_baseline), by(countryname)
egen year_endline=max(year) if h7a!=., by(countryname)
egen year_endline2=mean(year_endline), by(countryname)

gen reform_time_eligible=1 if year>=year_baseline2&year<year_endline2
*Choice:
*All reforms implemented in the year of baseline survey are included
*All reforms implemented in the year of endline survey are excluded

*same results with other choices: eg: gen reform_time_eligible=1 if year>year_baseline2&year<year_endline2

**cleaning:
replace reportingorg="World Bank" if (reform_access_wb==1 | reform_speed_wb==1 | reform_quality_wb==1)
replace reportingorg="USAID" if (reform_access_usaid==1 | reform_speed_usaid==1 | reform_quality_usaid==1)

*Put 1 for WB and USAID reforms:
replace reform_access=1 if reportingorg=="World Bank"|reportingorg=="USAID"
replace reform_speed=1 if reportingorg=="World Bank"|reportingorg=="USAID"
replace reform_quality=1 if reportingorg=="World Bank"|reportingorg=="USAID"

gen total_budget=totalcommitment
replace total_budget=totalcommitment_wb if totalcommitment_wb>0&totalcommitment_wb!=.
replace total_budget=totalcommitment_usaid if totalcommitment_usaid>0&totalcommitment_usaid!=.

sort countryname year iatiidentifier
****Creation of table describing judicial reforms for Kenya for the paper:
*browse iatiidentifier year_endline2 year_baseline2 countryname year reportingorg description reform_access reform_speed reform_quality total_budget if countryname=="Kenya" & reform_time_eligible==1 & (reform_access==1 | reform_speed==1 | reform_quality==1 | totalcommitment_wb!=. | totalcommitment_usaid!=.)

*browse iatiidentifier year_endline2 year_baseline2 countryname year reportingorg description reform_access reform_speed reform_quality total_budget if countryname=="Macedonia, FYR" & reform_time_eligible==1 & (reform_access==1 | reform_speed==1 | reform_quality==1 | totalcommitment_wb!=. | totalcommitment_usaid!=.)


***For all countries:
*browse iatiidentifier year_endline2 year_baseline2 countryname year reportingorg description reform_access reform_speed reform_quality total_budget if reform_time_eligible==1 & (reform_access==1 | reform_speed==1 | reform_quality==1 | totalcommitment_wb!=. | totalcommitment_usaid!=.)

*browse iatiidentifier year_endline2 year_baseline2 countryname year reportingorg description reform_access reform_speed reform_quality total_budget if countryname=="Lao PDR" & reform_time_eligible==1 & (reform_access==1 | reform_speed==1 | reform_quality==1 | totalcommitment_wb!=. | totalcommitment_usaid!=.)

save "Enterprise surveys_judicial_reforms.dta", replace

***Now create a dataset describing reforms for all countries:
keep if reform_time_eligible==1 & (reform_access==1 | reform_speed==1 | reform_quality==1)

keep reform_time_eligible year_endline2 year_baseline2 iatiidentifier countryname year reportingorg description reform_access reform_speed reform_quality total_budget
*keep if countryname=="Armenia"

*browse

save "List_judicial_reforms.dta", replace

***Reopen full dataset to work on all reforms:
use "Enterprise surveys_judicial_reforms.dta"



****calculate the sum of the reforms
local list_type_reform access speed quality
foreach var of local list_type_reform{
	egen reform_`var'_sum=sum(reform_`var') if reform_time_eligible==1, by (countryname)

	*sum of budget for reform:
	egen temp=sum(totalcommitment) if reform_`var'==1&reform_time_eligible==1, by (countryname)
	egen reform_`var'_budget=max(temp) if reform_time_eligible==1, by (countryname)
	replace reform_`var'_budget=0 if reform_`var'_budget==.
	drop temp
	}	

****calculate the sum of the reforms WB
local list_type_reform access_wb speed_wb quality_wb
foreach var of local list_type_reform{
	egen reform_`var'_sum=sum(reform_`var') if reform_time_eligible==1, by (countryname)
	
	*sum of budget for WB reform:
	egen temp=sum(totalcommitment_wb) if reform_`var'==1&reform_time_eligible==1, by (countryname)
	egen reform_`var'_budget=max(temp) if reform_time_eligible==1, by (countryname)
	drop temp
	}	

****calculate the sum of the reforms USAID
local list_type_reform access_usaid speed_usaid quality_usaid
foreach var of local list_type_reform{
	egen reform_`var'_sum=sum(reform_`var') if reform_time_eligible==1, by (countryname)
	
	*sum of budget for USAID reform:
	egen temp=sum(totalcommitment_usaid) if reform_`var'==1&reform_time_eligible==1, by (countryname)
	egen reform_`var'_budget=max(temp) if reform_time_eligible==1, by (countryname)
	drop temp
	}	


	
	
	
	
****calculate the sum of the reforms DB
local list_type_reform access_db speed_db quality_db
foreach var of local list_type_reform{
	egen reform_`var'_sum=sum(reform_`var') if reform_time_eligible==1, by (countryname)
	}	

	
	
****calculate total budget
egen temp=sum(totalcommitment) if reform_time_eligible==1 & (reform_access==1 | reform_speed==1 | reform_quality==1), by (countryname)
egen reform_budget=max(temp) if reform_time_eligible==1, by (countryname)
drop temp
*In million dollars:
*browse countryname reform_access reform_speed reform_quality reform_time_eligible totalcommitment reform_budget if countryname=="Rwanda"
browse countryname reform_access reform_speed reform_quality reform_time_eligible totalcommitment reform_budget if countryname=="Uganda"


*browse reform_budget
	
****calculate total budget WB
egen temp=sum(totalcommitment_wb) if reform_time_eligible==1, by (countryname)
egen reform_wb_budget=max(temp) if reform_time_eligible==1, by (countryname)
drop temp
*In million dollars:
replace reform_wb_budget=reform_wb_budget/1000000
		
****calculate total budget usaid
egen temp=sum(totalcommitment_usaid) if reform_time_eligible==1, by (countryname)
egen reform_usaid_budget=max(temp) if reform_time_eligible==1, by (countryname)
drop temp
*In million dollars:
replace reform_usaid_budget=reform_usaid_budget/1000000
		
*browse countryname year post court_good_small reform_access totalcommitment reform_access_budget reform_budget reform_access_wb totalcommitment_wb reform_access_wb_budget reform_wb_budget reform_access_db if countryname=="Kenya"		
*browse countryname year post court_good_small reform_access_usaid totalcommitment_usaid reform_access_usaid_budget reform_usaid_budget if countryname=="Kenya"		

*browse countryname year post court_good_small reform_access_usaid totalcommitment_usaid reform_access_usaid_budget reform_usaid_budget if countryname=="Albania"		
*browse countryname year post court_good_small reform_access_usaid totalcommitment_usaid reform_access_usaid_budget reform_usaid_budget if countryname=="Benin"

	

**keep only when we have entreprise surveys data


***Generate a variable of the year of any reform:
*browse countryname year court_good_small totalcommitment_wb totalcommitment_usaid if countryname=="Kenya"
gen temp=year if reform_access==1|reform_speed==1|reform_quality==1|reform_access_wb==1|reform_speed_wb==1|reform_quality_wb==1|reform_access_usaid==1|reform_speed_usaid==1|reform_quality_usaid==1
egen year_reform=min(temp), by(countryname)
drop temp

*the above commands are egen, they generate variables for the whole country
*The below command only keeps the years in which there is data on the courts
keep if h7a~=.

*browse court_good_small year countryname reform_access_sum reform_speed_sum reform_quality_sum reform_budget if countryname=="Rwanda"

*Calculate duration between two waves:
gen duration=year-year[_n-1] if countryname==countryname[_n-1]
replace duration=duration[_n+1] if countryname==countryname[_n+1]

***generate dummies for reforms:
local list_type_reform access speed quality access_wb speed_wb quality_wb access_usaid speed_usaid quality_usaid
foreach var of local list_type_reform{
drop reform_`var'

***sum of all reforms per year:
replace reform_`var'_sum=reform_`var'_sum[_n-1] if countryname==countryname[_n-1]
replace reform_`var'_sum=reform_`var'_sum/duration
gen reform_`var'_sum_post=reform_`var'_sum*post

***dichotomous variable for reform:
gen reform_`var'_dum=1 if reform_`var'_sum>0&reform_`var'_sum!=.
replace reform_`var'_dum=0 if reform_`var'_sum==0|reform_`var'_sum==.
gen reform_`var'_dum_post=reform_`var'_dum*post

***Budget reforms per year:
replace reform_`var'_budget=reform_`var'_budget[_n-1] if countryname==countryname[_n-1]
replace reform_`var'_budget=reform_`var'_budget/duration
*In million dollars:
replace reform_`var'_budget=reform_`var'_budget/1000000
*replace budget by 0 in the baseline:
replace reform_`var'_budget=0 if post==0
gen reform_`var'_budget_post=reform_`var'_budget*post


}


***generate dummies for reforms DB:
local list_type_reform access_db speed_db quality_db
foreach var of local list_type_reform{
drop reform_`var'

***sum of all reforms per year:
replace reform_`var'_sum=reform_`var'_sum[_n-1] if countryname==countryname[_n-1]
replace reform_`var'_sum=reform_`var'_sum/duration
gen reform_`var'_sum_post=reform_`var'_sum*post

***dichotomous variable for reform:
gen reform_`var'_dum=1 if reform_`var'_sum>0&reform_`var'_sum!=.
replace reform_`var'_dum=0 if reform_`var'_sum==0
gen reform_`var'_dum_post=reform_`var'_dum*post

}



gen reform_dum=1 if reform_access_dum==1|reform_speed_dum==1|reform_quality_dum==1
replace reform_dum=0 if reform_dum==.
gen reform_dum_post=reform_dum*post

gen reform_sum=reform_access_sum+reform_speed_sum+reform_quality_sum
gen reform_sum_post=reform_sum*post

***Budget reforms:
replace reform_budget=reform_budget[_n-1] if countryname==countryname[_n-1]
*replace budget by 0 in the baseline:
replace reform_budget=0 if post==0
*per year:
replace reform_budget=reform_budget/duration
gen reform_budget_post=reform_budget*post

***same for world bank and USAID:
local list_donor wb usaid
foreach var of local list_donor{

gen reform_`var'_dum=1 if reform_access_`var'_dum==1|reform_speed_`var'_dum==1|reform_quality_`var'_dum==1
replace reform_`var'_dum=0 if reform_`var'_dum==.
gen reform_`var'_dum_post=reform_`var'_dum*post
count if reform_`var'_dum_post==1

gen reform_`var'_sum=reform_access_`var'_sum+reform_speed_`var'_sum+reform_quality_`var'_sum
gen reform_`var'_sum_post=reform_`var'_sum*post


***Budget reforms:
replace reform_`var'_budget=reform_`var'_budget[_n-1] if countryname==countryname[_n-1]
*per year:
replace reform_`var'_budget=reform_`var'_budget/duration
gen reform_`var'_budget_post=reform_`var'_budget*post

}

****There are two exceptions to this:
***World Bank Nigeria:
replace reform_wb_dum=0 if countryname=="Nigeria"
*The World Bank reform is solely a legal aid access intervention in one state
***USAID El Salvador:
replace reform_usaid_dum=0 if countryname=="El Salvador"
*A small USAID project on mediation (see USAID/El salvador Tetra tech mediation.pdf)


***same for Doing Business:
gen reform_db_dum=1 if reform_access_db_dum==1|reform_speed_db_dum==1|reform_quality_db_dum==1
replace reform_db_dum=0 if reform_db_dum==.
gen reform_db_dum_post=reform_db_dum*post

gen reform_db_sum=reform_access_db_sum+reform_speed_db_sum+reform_quality_db_sum
gen reform_db_sum_post=reform_db_sum*post

gen reform_db_dum_big=1 if reform_access_db_dum==1&reform_speed_db_dum==1&reform_quality_db_dum==1
replace reform_db_dum_big=0 if reform_db_dum_big==.
gen reform_db_dum_big_post=reform_db_dum_big*post

gen reform_db_dum_small=1 if (reform_access_db_dum==1|reform_speed_db_dum==1|reform_quality_db_dum==1) & reform_db_dum_big==0
replace reform_db_dum_small=0 if reform_db_dum_small==.
gen reform_db_dum_small_post=reform_db_dum_small*post


*browse countryname year duration reform_budget

*browse countryname year post court_good_small reform_access_sum reform_access_dum reform_access_dum_post if countryname=="Kenya"
*browse countryname year post court_good_small reform_access_sum reform_access_dum reform_access_dum_post if countryname=="Angola"
*browse countryname year post court_good_small reform_access_sum reform_access_dum reform_access_dum_post if countryname=="Cambodia"
*browse countryname year post court_good_small reform_access_sum reform_access_dum reform_access_dum_post if countryname=="Uganda"

*browse countryname year post court_good_small reform_access_dum reform_speed_dum reform_quality_dum
*browse countryname year post court_good_small reform_access_sum reform_speed_sum reform_quality_sum reform_sum
*browse countryname year post court_good reform_access_budget reform_speed_budget reform_quality_sum reform_budget

tab reform_dum if post==1, missing 
tab reform_sum if post==1, missing 
*hist reform_budget if post==1 
*hist reform_budget if post==1 & reform_budget < 200
*hist reform_budget if post==1 & reform_budget < 50

sum reform_budget if post==1
sum reform_budget if post==1 & reform_budget>0 & reform_budget!=.
**take out Afhghanistan, a clear outlier
sum reform_budget if post==1 & reform_budget>0 & reform_budget!=. & countryname!="Afghanistan"

***replace budget by zero if no reform:

replace reform_budget=0 if reform_dum==0

local list_type_reform access speed quality access_wb speed_wb quality_wb access_usaid speed_usaid quality_usaid
foreach var of local list_type_reform{
	replace reform_`var'_budget=0 if reform_`var'_dum==0
	}
	
	
*browse countryname year reform_dum reform_budget reform_access_dum  reform_access_budget reform_wb_dum reform_access_wb_budget

*sort reform_budget
*browse countryname reform_budget

save "Enterprise surveys_judicial_reforms.dta", replace

sort countryname year
merge countryname year using "GDP per capita_clean2.dta"
***drop the observations in World Development indicators but not in Entreprise surveys
drop if _merge==2
drop _merge

sort countryname year
merge countryname year using "Foreign aid_clean.dta"
*browse countryname gdp* _merge
***drop the observations in World Development indicators but not in Entreprise surveys
drop if _merge==2
drop _merge

sort countryname year
merge countryname year using "Doing business_Full data_clean.dta"
*browse countryname year court_good_small dtf _merge
drop if _merge==2
drop _merge

sort countryname year
merge countryname year using "PPP exchange rate2_clean.dta"
*browse countryname year court_good_small dtf _merge
drop if _merge==2
drop _merge


*now merge with polity IV
sort countryname 
merge countryname using "Polity IV.dta"
*browse countryname year court_good_small polity2 _merge
drop if _merge==2
drop _merge


*now merge with other reforms from IATI
sort countryname 
merge countryname using "Enterprise surveys_Other_reforms.dta"
*browse countryname year court_good_small reform_education_budget_gdp _merge
drop if _merge==2
drop _merge

****For the 8 critical countries (Malawi, Nigeria, Pakistan, Rwanda, Tanzania, Uganda, Vietnam, Zambia) 
****In which there was a reform targeting all 3 characteristics (access, speed, quality)
****The judiciary's budget is a critical determinant of whether the country received a comprehensive reform or not
****For these 8 countries, I searched  https://www.internationalbudget.org/open-budget-survey/
****To get the judciary's budget

*now merge with Budget judiciary
sort countryname 
merge countryname using "Budget.dta"
*browse countryname year budget_us _merge
drop if _merge==2
drop _merge

*now merge with pbetaT-l:
sort countryname 
merge countryname using "Doing business_p_betaT_l.dta"
*browse countryname year sueing_condition _merge
drop if _merge==2
drop _merge


*browse if countryname=="Kenya"
*browse countryname year  pppconversionfactorgdplcuperinte

*sort countryname year
browse countryname year reform_budget gdppppconstant2011internationaln
**We take gdp in the baseline year


*reform_budget is in million dollars:
gen reform_budget_gdp=reform_budget*1000000/gdppppconstant2011internationaln[_n-1] if countryname==countryname[_n-1]
replace reform_budget_gdp=0 if reform_budget_gdp==.
*browse reform_budget_gdp reform_budget gdppppconstant2011internationaln if countryname=="Kenya"
gen reform_budget_gdp_post=reform_budget_gdp*post

****reform_budget as a proportion of budget judiciary
***median budget judicairy OECD=0.2% (see OECD FINAL Civil Justice Policy Paper.pdf in budget judiciary)
gen reform_budget_jud=reform_budget/(0.2/100*gdppppconstant2011internationaln[_n-1]) if countryname==countryname[_n-1]

****For the 8 critical countries (Malawi, Nigeria, Pakistan, Rwanda, Tanzania, Uganda, Vietnam, Zambia) 
****In which there was a reform targeting all 3 characteristics (access, speed, quality)
****The judiciary's budget is a critical determinant of whether the country received a comprehensive reform or not
****For these 8 countries, I searched  https://www.internationalbudget.org/open-budget-survey/
****To get the judciary's budget

replace reform_budget_jud=reform_budget/budget_us if budget_us!=.
replace reform_budget_jud=0 if reform_budget_jud==.

*browse countryname reform_budget reform_budget_jud if countryname=="Nigeria"
*browse reform_budget_gdp reform_budget gdppppconstant2011internationaln reform_budget_jud if countryname=="Kenya"


gen reform_budget_jud_post=reform_budget_jud*post
*hist reform_budget_jud if post==1
*hist reform_budget_jud if post==1 & reform_budget_jud<=1
*hist reform_budget_jud if post==1 & reform_budget_jud>=1&reform_budget_jud<=2
sort reform_budget_jud

sort countryname year
/*
browse countryname year gdppppconstant2011internationaln reform_budget reform_budget_jud ///
 if countryname=="Malawi"|countryname=="Nigeria"|countryname=="Pakistan"|countryname=="Rwanda" ///
 |countryname=="Tanzania"|countryname=="Vietnam"|countryname=="Uganda"|countryname=="Zambia"
*/



gen reform_dum_big1=1 if reform_budget_jud>=0.05&reform_budget_jud!=.&(reform_access_dum==1&reform_speed_dum==1&reform_quality_dum==1)
replace reform_dum_big1=0 if reform_dum_big1==.
gen reform_dum_big1_post=reform_dum_big1*post

/*now generate small reforms: 
-No world bank, no usaid
-piecemeal reforms
-small budget
*/

gen reform_dum_small1=1 if reform_dum==1 & (reform_wb_dum==0 & reform_usaid_dum==0) & ((reform_budget_jud>=0&reform_budget_jud<0.05)|(reform_access_dum==0|reform_speed_dum==0|reform_quality_dum==0))
*Small = A reform AND (not WB, not USAID) AND (small budget OR one criteria missing)
replace reform_dum_small1=0 if reform_dum_small1==.

*Budget: 3%
gen reform_dum_big2=1 if reform_budget_jud>=0.03&reform_budget_jud!=.&(reform_access_dum==1&reform_speed_dum==1&reform_quality_dum==1)
replace reform_dum_big2=0 if reform_dum_big2==.
gen reform_dum_big2_post=reform_dum_big2*post

gen reform_dum_small2=1 if reform_dum==1 & (reform_wb_dum==0 & reform_usaid_dum==0) & ((reform_budget_jud>=0&reform_budget_jud<0.03)|(reform_access_dum==0|reform_speed_dum==0|reform_quality_dum==0))
replace reform_dum_small2=0 if reform_dum_small2==.

*Budget: 7%
gen reform_dum_big3=1 if reform_budget_jud>=0.07&reform_budget_jud!=.&(reform_access_dum==1&reform_speed_dum==1&reform_quality_dum==1)
replace reform_dum_big3=0 if reform_dum_big3==.
gen reform_dum_big3_post=reform_dum_big3*post

gen reform_dum_small3=1 if reform_dum==1 & (reform_wb_dum==0 & reform_usaid_dum==0) & ((reform_budget_jud>=0&reform_budget_jud<0.07)|(reform_access_dum==0|reform_speed_dum==0|reform_quality_dum==0))
replace reform_dum_small3=0 if reform_dum_small3==.


*Budget: 1%
gen reform_dum_big4=1 if reform_budget_jud>=0.01&reform_budget_jud!=.&(reform_access_dum==1&reform_speed_dum==1&reform_quality_dum==1)
replace reform_dum_big4=0 if reform_dum_big4==.
gen reform_dum_big4_post=reform_dum_big4*post

gen reform_dum_small4=1 if reform_dum==1 & (reform_wb_dum==0 & reform_usaid_dum==0) & ((reform_budget_jud>=0&reform_budget_jud<0.01)|(reform_access_dum==0|reform_speed_dum==0|reform_quality_dum==0))
replace reform_dum_small4=0 if reform_dum_small4==.


forvalues i=1(1) 4{
	gen reform_dum`i'=1 if reform_wb_dum==1|reform_usaid_dum==1|reform_dum_big`i'==1
	*reform_wb_dum==1|reform_usaid_dum==1|reform_dum_big`i'==1
	
	*Now reform_dum`i' is constant for the country:
	sort countryname post
	replace reform_dum`i'=reform_dum`i'[_n+1] if countryname ==countryname[_n+1]&post==0
	
	replace reform_dum`i'=0 if reform_dum`i'==.
	gen reform_dum`i'_post=reform_dum`i'*post

	*sum of reforms:
	egen reform_sum`i'=rowtotal(reform_wb_dum reform_usaid_dum reform_dum_big`i')
	gen reform_sum`i'_post=reform_sum`i'*post
		
	gen reform_access_dum`i'=1 if reform_access_dum==1|reform_access_wb_dum==1|reform_access_usaid_dum==1
	gen reform_speed_dum`i'=1 if reform_speed_dum==1|reform_speed_wb_dum==1|reform_speed_usaid_dum==1
	gen reform_quality_dum`i'=1 if reform_quality_dum==1|reform_quality_wb_dum==1|reform_quality_usaid_dum==1
	
	}

	
forvalues i=1(1) 4{
	***same for reform_small:
	sort countryname post
	replace reform_dum_small`i'=reform_dum_small`i'[_n+1] if countryname ==countryname[_n+1]&post==0
	replace reform_dum_small`i'=0 if reform_dum_small`i'==.
	gen reform_dum_small`i'_post=reform_dum_small`i'*post
	}


	
gsort -reform_dum1
*browse countryname reform_dum1  reform_access_dum1 reform_speed_dum1 reform_quality_dum1 if post==1

gsort -reform_dum2
*browse countryname reform_dum2  reform_access_dum2 reform_speed_dum2 reform_quality_dum2 if post==1

gsort -reform_dum3
*browse countryname reform_dum3  reform_access_dum3 reform_speed_dum3 reform_quality_dum3 if post==1



****Same for world bank and usaid:

local list_donor wb usaid
foreach var of local list_donor{

*reform_budget is in million dollars:
gen reform_`var'_budget_gdp=reform_`var'_budget*1000000/gdppppconstant2011internationaln
*browse reform_`var'_budget_gdp reform_`var'_budget gdppppconstant2011internationaln if countryname=="Kenya"
gen reform_`var'_budget_gdp_post=reform_`var'_budget_gdp*post

****reform_`var'_budget as a proportion of budget jud
***median budget judicairy OECD=0.2%
gen reform_`var'_budget_jud=reform_`var'_budget*1000000/(0.2/100*gdppppconstant2011internationaln)
*browse reform_`var'_budget_gdp reform_`var'_budget gdppppconstant2011internationaln reform_`var'_budget_jud if countryname=="Kenya"


*browse reform_budget_jud reform_`var'_budget_gdp reform_`var'_budget gdppppconstant2011internationaln reform_`var'_budget_jud if countryname=="Afghanistan"

gen reform_`var'_budget_jud_post=reform_`var'_budget_jud*post
*hist reform_`var'_budget_jud if post==1

}



***For each type of reform
local list_type_reform access speed quality access_wb speed_wb quality_wb access_usaid speed_usaid quality_usaid
foreach var of local list_type_reform{
	***Budget reforms per year:
	gen reform_`var'_gdp=reform_`var'_budget*1000000/gdppppconstant2011internationaln
	gen reform_`var'_gdp_post=reform_`var'_gdp*post

	****reform_budget as a proportion of budget judiciary
	gen reform_`var'_jud=reform_`var'_budget*1000000/(0.2/100*gdppppconstant2011internationaln)
	gen reform_`var'_jud_post=reform_`var'_jud*post

	}

*browse countryname year duration reform_budget_gdp reform_budget_gdp_post reform_access_gdp reform_access_gdp_post reform_budget reform_access_gdp gdppppconstant2011internationaln if countryname=="Kenya"

label variable reform_budget "Budget of judicial reforms per year in million dollars"
label variable reform_budget_gdp "Budget of judicial reforms (as a fraction of GDP)"
label variable reform_budget_jud "Budget of judicial reforms (as a fraction of judiciary's budget)"


*browse countryname year reform_budget_jud gdppppconstant2011internationaln if countryname=="Nigeria"

egen reform_budget_jud2=rowtotal(reform_budget_jud reform_wb_budget_jud reform_usaid_budget_jud)
label variable reform_budget_jud2 "Total budget of judicial reforms (as a fraction of judiciary's budget)"

gen reform_budget_jud2_post=reform_budget_jud2*post



*sort reform_budget_gdp
*browse countryname reform_budget_gdp

*browse countryname year reform_dum reform_budget gdppppconstant2011internationaln

*browse countryname year reform_dum reform_budget_jud reform_wb_dum reform_wb_budget_jud if post==1

***Create a measur of foreign aid per gdp
gen foreign_aid_gdp=foreign_aid/gdppppconstant2011internationaln
gen foreign_aid_percap=foreign_aid/populationtotalsppoptotl
 
gen foreign_aid2=foreign_aid
replace foreign_aid2=0 if foreign_aid2==.
gen foreign_aid2_gdp=foreign_aid2/gdppppconstant2011internationaln
gen foreign_aid2_percap=foreign_aid2/populationtotalsppoptotl


sort countryname year
*browse countryname year foreign_aid gdppppconstant2011internationaln foreign_aid_gdp foreign_aid_percap foreign_aid2_gdp foreign_aid2_percap



***descriptive stats on reform:
codebook reform_wb_dum_post reform_wb_budget_jud_post
sum reform_wb_budget_jud if reform_wb_dum==1
sum reform_usaid_budget_jud if reform_usaid_dum==1
*Budgets of WB and USAId is 6% of budget judiciary

sum reform_wb_budget_gdp if reform_wb_dum==1
sum reform_usaid_budget_gdp if reform_usaid_dum==1
*Budgets of WB and USAId is 0.01% of GDP
*Logical since budget judiciary=0.2% of GDP, so 0.05*0.02=0.0001, hence 0.01%

sum reform_wb_budget if reform_wb_dum==1
sum reform_usaid_budget if reform_usaid_dum==1



***Now create a dataset to be merged with microdata:
keep countryname year post h7a reform_dum reform_dum_post reform_sum_post ///
 reform_dum1 reform_dum2 reform_dum3 reform_dum4 /// 
 reform_sum* ///
 reform_dum_big1 reform_dum_big2 reform_dum_big3 reform_dum_big4 /// 
 reform_dum1_post reform_dum2_post reform_dum3_post reform_dum4_post /// 
 reform_budget_jud reform_budget_jud2 reform_budget_jud2_post /// 
 reform_access_dum_post reform_speed_dum_post reform_quality_dum_post ///
 reform_access_sum_post reform_speed_sum_post reform_quality_sum_post /// 
 reform_budget_jud_post  reform_budget_gdp_post ///
 reform_access_jud_post reform_speed_jud_post reform_quality_jud_post ///
 reform_access_gdp_post reform_speed_gdp_post reform_quality_gdp_post ///
 reform_wb_dum_post reform_wb_sum_post ///
 reform_access_wb_dum_post reform_speed_wb_dum_post reform_quality_wb_dum_post ///
 reform_access_wb_sum_post reform_speed_wb_sum_post reform_quality_wb_sum_post /// 
 reform_wb_budget_jud_post  reform_wb_budget_gdp_post ///
 reform_access_wb_jud_post reform_speed_wb_jud_post reform_quality_wb_jud_post ///
 reform_access_wb_gdp_post reform_speed_wb_gdp_post reform_quality_wb_gdp_post ///
 reform_usaid_dum_post reform_usaid_sum_post ///
 reform_access_usaid_dum_post reform_speed_usaid_dum_post reform_quality_usaid_dum_post ///
 reform_access_usaid_sum_post reform_speed_usaid_sum_post reform_quality_usaid_sum_post /// 
 reform_usaid_budget_jud_post  reform_usaid_budget_gdp_post ///
 reform_access_usaid_jud_post reform_speed_usaid_jud_post reform_quality_usaid_jud_post ///
 reform_access_usaid_gdp_post reform_speed_usaid_gdp_post reform_quality_usaid_gdp_post ///
 reform_db_dum_post reform_db_sum_post ///
 reform_access_db_dum_post reform_speed_db_dum_post reform_quality_db_dum_post ///
 reform_access_db_sum_post reform_speed_db_sum_post reform_quality_db_sum_post /// 
 log_gdppercapita2 log_s log_ngd log_school foreign_aid foreign_aid_gdp foreign_aid_percap foreign_aid2 foreign_aid2_gdp foreign_aid2_percap dtf pppconversionfactorgdplcuperinte gdppppconstant2011internationaln populationtotalsppoptotl ///
 year_reform year_baseline year_baseline2 year_endline year_endline2 ///
 reform_dum_small1 reform_dum_small1_post reform_dum_small2 reform_dum_small2_post reform_dum_small3 reform_dum_small3_post reform_dum_small4 reform_dum_small4_post ///
 reform_budget_jud2 ///
 reform_db_dum reform_db_dum_post reform_db_dum_big reform_db_dum_big_post reform_db_dum_small reform_db_dum_small_post ///
 polity2 Autocracy Anocracy Democracy ///
 reform_* ///
 sueing_condition no_renegotiation_condition


*browse countryname year court_good reform_dum2_post if countryname=="Sweden"|countryname=="Estonia"|countryname=="Czech Republic" ///
|countryname=="Slovak Republic"|countryname=="Slovenia"|countryname=="Hungary"|countryname=="Poland"

*browse countryname year court_good reform_dum2_post

***generate a variable identifying countries where: 1) data is available, 2) for 2 years:
sort countryname year
gen dataset=1 if (countryname==countryname[_n+1]&h7a!=.) | (countryname==countryname[_n-1]&h7a!=.)

*browse countryname year court_good reform_wb_dum reform_usaid_dum reform_dum_big7 reform_sum7 reform_budget_jud2 if dataset==1

*keep if reform_dum_big5==1
*keep if reform_wb_dum==1

*browse countryname year reform_dum4 reform_dum_small4 if dataset==1

*browse countryname year reform_db_dum reform_db_dum_big reform_db_dum_small if dataset==1

*browse countryname year reform_dum4 reform_dum_big4 if countryname=="Uganda"

*browse countryname year reform_dum4 reform_dum_big4 if countryname=="Benin"


****Correlation between judicial reforms and other reforms
*browse countryname post reform_dum4_post reform_edu_percap_post if post==1

reg reform_dum4_post reform_jud2_gdp_post if post==1
sum reform_edu_gdp_post if post==1
local mean_dep_var=r(mean)
local sd_dep_var=r(sd)
outreg2 using "Table_D1.tex", tex label adec(2) addstat("Mean control group", `mean_dep_var', "(SD)", `sd_dep_var') bdec(2) se sdec(2) ctitle("Judicial reform") replace

local list_reform_type edu health trans nrj bank indus trade debt tax confl parlia elec media corrup 
foreach var of local list_reform_type{
	reg reform_dum4_post reform_`var'_gdp_post if post==1
	sum reform_`var'_gdp_post if post==1
	local mean_dep_var=r(mean)
	local sd_dep_var=r(sd)
	outreg2 using "Table_D1.tex", tex label adec(2) addstat("Mean control group", `mean_dep_var', "(SD)", `sd_dep_var') bdec(2) se sdec(2) ctitle("Judicial reform") append
	}


*browse countryname reform_dum4_post reform_debt_gdp_post if post==1
*browse countryname reform_dum4_post reform_debt_gdp_post if countryname=="Macedonia, FYR"


***Browse the sum of reforms:
*browse countryname reform_dum4_post reform_dum_small4_post reform_sum_post
****generate reform_sum_post only for countries with limited reforms:
gen reform_sum_limited_post=reform_sum_post if reform_dum_small4_post==1
replace reform_sum_limited_post=0 if reform_sum_limited_post==.&reform_dum4_post!=.

gen reform_sum_comp_post=reform_sum_post if reform_dum4_post==1
replace reform_sum_comp_post=0 if reform_sum_comp_post==.&reform_dum4_post!=.

*browse countryname reform_dum4_post reform_dum_small4_post if countryname=="Tanzania"

****desc stats on sueing_condition
sum sueing_condition if post==1&dataset==1
gen sueing_condition_pos=1 if sueing_condition>=0
replace sueing_condition_pos=0 if sueing_condition<0
sum sueing_condition_pos if post==1&dataset==1
drop sueing_condition_pos

sort countryname year
save "Enterprise surveys_judicial_reforms_for merge with standardized dataset.dta", replace




exit

